package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService{

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${pagepath}")
    private String pagePath;

    public void createHtml(String spuId) {
        //获取context对象,用于存储商品的相关数据
        Context context=new Context();

        //获取静态化页面的相关数据
        Map<String,Object> itemDate =this.getItemDate(spuId);
        context.setVariables(itemDate);

        //获取商品详情页面的存储位置
        File dir =new File(pagePath);
        //判断当前存储位置的文件是否存在,如果不存在,则新建
        if(!dir.exists()){
            dir.mkdirs();
        }
        //定义输出流完成文件的生成
        File file =new File(dir+"/"+spuId+".html");

        Writer out=null;

        try {
            out =new PrintWriter(file);
            //生成静态化页面
            /*
            * process:需要三个参数
            *void process(TemplateSpec var1, IContext var2, Writer var3);
            第一个参数 模板的名称  context 对象  一个输出流
            * */
            templateEngine.process("item",context,out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //获取静态化页面的相关数据
    private Map<String, Object> getItemDate(String spuId) {
        Map<String, Object> resultMap =new HashMap<>();
        //获取spu
        Spu spu = spuFeign.findById(spuId).getData();
        resultMap.put("spu",spu);

        //获取图片
        if(spu!=null){
           //再进行判断
            if(StringUtils.isNotEmpty(spu.getImages())){
               resultMap.put("imageList",spu.getImages().split(","));
            }
        }
        //获取商品的分类信息 共有三级分类
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();

        resultMap.put("category1",category1);
        resultMap.put("category2",category2);
        resultMap.put("category3",category3);

        //获取sku的相关信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        resultMap.put("skuList",skuList);

        //获取商品的规格信息
        resultMap.put("specimformation", JSON.parseObject(spu.getSpecItems(),Map.class));
        return resultMap;
    }
}
